package com.t.bricks.bussiness.db.dao.sendmsg.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.sendmsg.SendMsgDao;
import com.t.bricks.bussiness.db.entity.sendmsg.SendMsg;
import com.t.bricks.bussiness.db.mapper.sendmsg.SendMsgMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class SendMsgDaoImpl implements SendMsgDao<SendMsg> {
	
	@Autowired
	private SendMsgMapper sendMsgMapper;
	
	/**
	 * 查找待发送的对象集合, 取'待发送'及'发送中'状态维持了10分钟的
	 * @param iErrorMax 错误次数上限
	 * @return
	 */
	@Override
	public MsgEmity findByWait(Integer iErrorMax) {
		List<SendMsg> list = null;
		try {
			list = sendMsgMapper.findByWait(iErrorMax);
			if (null == list) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
			if (list.size() < 1) {
				return new MsgEmity(true, "查询成功，但没有数据！", list);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}
	
	/**
	 * 累加错误次数
	 * @param sId
	 * @param iErrorMax
	 * @param sMemo
	 * @return
	 */
	@Override
	public MsgEmity addError(String sId, Integer iErrorMax, String sMemo) {
		try {
			int i = sendMsgMapper.addError(sId, iErrorMax, sMemo);
			if (i < 1) {
				Log.error("累加错误次数未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "累加错误次数数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("累加错误次数时发生异常:", e.getMessage());
			return new MsgEmity(false, "累加错误次数失败！", 7002);
		}
		
		return new MsgEmity(true, "累加错误次数成功！", 7999);
	}
	
	/**
	 * 错误次数清零
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity clearError(String sId) {
		try {
			int i = sendMsgMapper.clearError(sId);
			if (i < 1) {
				Log.error("错误次数清零未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "错误次数清零失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("错误次数清零时发生异常:", e.getMessage());
			return new MsgEmity(false, "错误次数清零失败！", 7002);
		}
		
		return new MsgEmity(true, "错误次数清零成功！", 7999);
	}

}
